package com.xx.tools.controller;

import com.xx.common.annotation.Anonymous;
import com.xx.common.core.controller.BaseController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 工具Controller
 *
 * @author ruoyi
 * @date 2024-09-28
 */
@RestController
@RequestMapping("/account/ips")
public class IpController extends BaseController {

    @Anonymous
    @GetMapping("/get-ip")
    public String getClientIp(HttpServletRequest request) {
        // 1. 直接获取客户端 IP（可能被代理服务器修改）
        String ip = request.getHeader("X-Forwarded-For");

        // 2. 如果 X-Forwarded-For 不存在，则使用 REMOTE_ADDR
        if (ip == null || ip.isEmpty()) {
            ip = request.getRemoteAddr();
        }

        // 处理多个 IP 的情况（例如多层代理）
        if (ip.contains(",")) {
            ip = ip.split(",")[0].trim();
        }

        System.out.println("客户端 IP: " + ip);

        return "当前访问 IP 为: " + ip;
    }

}
